package com.asset.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * 通过该工具类生成对应的编号类型的流水号
 */
@Component
public class RedisUtils {
//    public RedisTemplate redisTemplate;
//    @Autowired
//    private StringRedisTemplate stringRedisTemplate;

    //定好key
    private final static String KEY = "serialNumber";

    //定好value的前两位
    private StringBuilder value = new StringBuilder("gz");

    public void setValue(StringBuilder value) {
        this.value = value;
    }

    // 设置过期时间24小时。单位为秒
    private static final long EXPIRE_TIME = 60*60*24;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //value gz+日期   score是流水号  两者结合生成购置编号
    public String createNumber() {

        //定好时间
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        StringBuilder format = new StringBuilder(sdf.format(date));

        //组合value
        StringBuilder append = value.append(format);
        //获取当前的key
        String key = append.toString();

        //将score自增1
        Double v = stringRedisTemplate.opsForZSet().incrementScore(key, append.toString(), 1);
        if (v != null && v < 2) {
            //设置过期时间
            stringRedisTemplate.expire(append.toString(),EXPIRE_TIME, TimeUnit.SECONDS);
        }
        //生成后半部分流水号为000001六位
        int number = v.intValue();
        String numberStr = String.valueOf(number);

        String formatted = "000000";
        if (numberStr.length() >= 7) {
            System.out.println("超过编号数量错误");
        }else {
            //将得到的号码对应的位置变换
            DecimalFormat df = new DecimalFormat("000000");
            formatted = df.format(number);
            //System.out.println(formatted); // 输出: 001234
        }
        //组合成编号
        append.append(formatted);

        //获取到购置的score的值
        Double score = stringRedisTemplate.opsForZSet().score(key, key);
        //最后将生成的编号放在redis中
        if (score != null) {
            insertRedis(key, append.toString(),score);
        }

        //System.out.println(append.toString());
        return append.toString();
    }

    //获取所有编码的方法
    public List<String> getNumber(){
        //先获取key
        String key = getKey();
        List<String> list = new ArrayList<String>();

        Set<String> stringSet = stringRedisTemplate.opsForZSet().reverseRange(key, 1, -1);
        if (stringSet != null && stringSet.size() > 0) {
            for (String string : stringSet) {
                list.add(string);
            }
        }

        return list;
    }

    //获取redis中的key,只能是当前时间，若过了0点则无法查询到之前的
    public String getKey(){

        //定好时间
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        StringBuilder format = new StringBuilder(sdf.format(date));

        String key = value.append(format).toString();

        return key;
    }

    //将编号放在redis中的方法
    //key为键，value为，score为当前自增的最大值
    public void insertRedis(String key, String maxValue,Double score) {
        //为了防止出现自增两次而只产生一次编号的问题（不知道有没有）,反正写了肯定安全
        int num = score.intValue();
        for (int i = 0; i < num; i++) {
            String formatted;
            //将得到的号码对应的位置变换
            DecimalFormat df = new DecimalFormat("000000");
            formatted = df.format(i+1);
            String addValue = key+formatted;

            stringRedisTemplate.opsForZSet().add(key, addValue,0);
        }
    }


}
